﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RBAC.Administry.Resource
{
    public partial class List : System.Web.UI.Page
    {
        #region page logic

        private void LoadData()
        {
            var data = RBAC.Resource.LoadAll();
            grid.DataSource = data;
            grid.DataBind();
        }

        private void Delete(string id)
        {
            RBAC.Resource resource = RBAC.Resource.Load(id);
            if (resource != null)
            {
                resource.Remove();
                resource.Save();

                LoadData();

                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "done", "done();", true);
            }
        }

        #endregion

        #region event processing

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        protected void grid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "_delete":
                    Delete(e.CommandArgument.ToString());
                    break;
            }
        }

        protected void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grid.PageIndex = e.NewPageIndex;
            LoadData();
        }
        #endregion
    }
}
